package com.lin.generate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.generate.common.base.PageReqVO;
import com.lin.generate.common.dto.UserDataSourceKey;
import com.lin.generate.common.utill.Asserts;
import com.lin.generate.common.utill.JsonUtils;
import com.lin.generate.common.utill.ObjectConversion;
import com.lin.generate.common.vo.request.DataSourceConfigReqVO;
import com.lin.generate.common.vo.response.DataSourceVO;
import com.lin.generate.common.vo.response.DataSourceConfigRespVO;
import com.lin.generate.entity.ZlDataSourceConfig;
import com.lin.generate.mapper.ZlDataSourceConfigMapper;
import com.lin.generate.service.IZlDataSourceConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;

/**
 * @description TODO
 * @Author: z156940l@outlook.com
 * @className DataSourceConfigServiceImpl
 * @Date: 2023/6/4 15:58
 */
@Slf4j
@Service
public class ZlDataSourceConfigServiceImpl extends ServiceImpl<ZlDataSourceConfigMapper, ZlDataSourceConfig> implements IZlDataSourceConfigService {
    @Override
    public Boolean save(DataSourceConfigReqVO vo) {
        log.info("# 开始添加数据源配置,请求信息:{}", JsonUtils.toJson(vo));
        ZlDataSourceConfig entity = new ZlDataSourceConfig();
        BeanUtils.copyProperties(vo, entity);
        this.save(entity);
        log.info("# 结束添加数据源配置。");
        return true;
    }

    @Override
    public Boolean update(DataSourceConfigReqVO vo) {
        log.info("# 开始修改数据源配置,请求信息:{}", JsonUtils.toJson(vo));
        ZlDataSourceConfig entity = this.getById(vo.getId());
        if (Objects.nonNull(entity)) {
            BeanUtils.copyProperties(vo, entity);
            this.updateById(entity);
            log.info("# 结束修改用户表。");
            return true;
        }
        log.info("# 修改数据源配置失败。");
        return false;
    }

    @Override
    public IPage<DataSourceConfigRespVO> pageList(PageReqVO pageReqVO, DataSourceConfigReqVO vo) {
        log.info("# 开始分页查询数据源配置信息。");
        if (pageReqVO.getIsAll() == 1L) {
            pageReqVO.setPageNum(0L);
            pageReqVO.setPageSize(Long.MAX_VALUE);
        }
        LambdaQueryWrapper<ZlDataSourceConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(ZlDataSourceConfig::getCreateTime);
        if (Objects.nonNull(vo)) {

        }
        Page<ZlDataSourceConfig> page = new Page<>(pageReqVO.getPageNum(), pageReqVO.getPageSize());
        IPage<ZlDataSourceConfig> entityIPage = this.page(page, wrapper);
        IPage<DataSourceConfigRespVO> resultIPage = new Page<>();
        List<DataSourceConfigRespVO> resultRecord = ObjectConversion.copy(entityIPage.getRecords(), DataSourceConfigRespVO.class);
        resultIPage.setRecords(resultRecord);
        resultIPage.setTotal(entityIPage.getTotal());
        log.info("# 结束分页查询数据源配置信息。");
        return resultIPage;
    }

    @Override
    public DataSourceConfigRespVO getObjectById(Long id) {
        DataSourceConfigRespVO result = new DataSourceConfigRespVO();
        ZlDataSourceConfig entity = this.getById(id);
        if (Objects.nonNull(entity)) {
            BeanUtils.copyProperties(entity, result);
        }
        return result;
    }

    @Override
    public DataSourceVO getDataSource(UserDataSourceKey key) {
        //TODO 目前只有通过userId获取数据源id
        DataSourceVO vo = new DataSourceVO();
        ZlDataSourceConfig config = this.lambdaQuery()
                .eq(ZlDataSourceConfig::getUserId, key.getUserId())
                .one();
        Asserts.notNull(config, "请先配置数据源，或配置使用当前数据源为默认数据源！");
        vo.setId(config.getId());
        vo.setUrl(config.getUrl());
        vo.setUserName(config.getUserName());
        vo.setPassWord(config.getPassWord());
        vo.setDriverClassName(config.getDriverClassName());
        vo.setDataSourceName(config.getDataSourceName());
        return vo;
    }
}
